<?php

namespace app\merchant\controller;

use think\Controller;
use think\Db;

class Common extends Controller
{
    public static $merchantId;

    public function _initialize()
    {
        if (!session('merchant')) {
            $this->redirect('index/login');
        }

        self::$merchantId = session('merchant.merchant_id');

        if (session('merchant.merchant_account_status') == 2) {
            //企业认证，店铺认证判断
            $enterpriseStatus = Db::name('enterprise')
                ->where('enterprise_merchant_id', self::$merchantId)
                ->where('enterprise_is_delete', 0)
                ->value('enterprise_verify_status');
            $storeStatus = Db::name('store')
                ->where('store_merchant_id', self::$merchantId)
                ->where('store_is_delete', 0)
                ->value('store_verify_status');
            if (empty($enterpriseStatus)) {
                $this->redirect('enterprise/create');
            } else if ($enterpriseStatus == 1) {
                if (empty($storeStatus)) {
                    $this->redirect('store/create');
                } else {
                    $this->redirect('enterprise/confirm');
                }
            } else if ($enterpriseStatus == 2) {
                if($storeStatus == 2) {
                    $this->redirect('index/active');
                } else {
                    $this->redirect('enterprise/confirm');
                }
            }
        }

        $store = Db::name('store')
            ->where('store_merchant_id', self::$merchantId)
            ->where('store_is_delete', 0)
            ->field('store_name, store_face_img')
            ->find();
        if (empty($store['store_face_img'])) {
            $store['store_face_img'] = 'default.jpg';
        }
        $this->assign([
            'store_name' => $store['store_name'] ? $store['store_name'] : null,
            'store_head' => config('oss_imgRootPath') . 'store/face/' . $store['store_face_img']."@!seller_store_face",
        ]);
    }

    /**
     * 查询物流接口
    */
    function logistics($com = 'auto', $nu)
    {
        $host = "https://ali-deliver.showapi.com";
        $path = "/showapi_expInfo";
        $method = "GET";
        $appcode = "1bf3b3c9756b40a6b3a6d19d9e6ef417";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "com=" . $com . "&nu=" . $nu;
        $bodys = "";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        if (1 == strpos("$" . $host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }


    



}